/*
  Copyright 2007 Anders Båtstrand

      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

 */

package org.dyndns.anderlin.jmplayer.model;

import javax.persistence.*;
import java.io.File;
import java.util.Date;
import java.util.Set;


@Entity
public class Track extends JmplayerObject {

    /**
     * The tracks number on the album
     */
    @Basic
    @Column(nullable = false)
    private Integer trackNumber;

    /**
     * The album this track belongs to
     */
    @ManyToOne
    @JoinColumn(nullable = false)
    private Album album;

    /**
     * The (part of a) piece this track is a recording of
     */
    @ManyToOne
    @JoinColumn(nullable = false)
    private Piece piece;

    /**
     * Was this recorded live (that is: during a concert)?
     */
    @Basic
    @Column(nullable = false)
    private Boolean live = false;

    /**
     * When was this recorded?
     */
    @Temporal(TemporalType.DATE)
    private Date recorded;

    /**
     * The artists performing on this track
     */
    @OneToMany(mappedBy = "recording", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<Performance> performances;

    /**
     * Duration in seconds
     */
    @Basic
    @Column(nullable = false)
    private Long duration;

    /**
     * A transient reference to the file
     */
    @Transient
    private File file;

    /**
     * Hash of the file (md5)
     */
    @Basic
    @Column(nullable = false)
    private String hash;

    public Integer getTrackNumber() {
        return trackNumber;
    }

    public void setTrackNumber(Integer trackNumber) {
        this.trackNumber = trackNumber;
    }


    public Piece getPiece() {
        return piece;
    }

    public void setPiece(Piece piece) {
        this.piece = piece;
    }

    public Album getAlbum() {
        return album;
    }

    public void setAlbum(Album album) {
        this.album = album;
    }

    public Boolean getLive() {
        return live;
    }

    public void setLive(Boolean live) {
        this.live = live;
    }

    public Set<Performance> getPerformances() {
        return performances;
    }

    public void setPerformances(Set<Performance> performances) {
        this.performances = performances;
    }

    public Long getDuration() {
        return duration;
    }

    public void setDuration(Long duration) {
        this.duration = duration;
    }


    public Date getRecorded() {
        return recorded;
    }

    public void setRecorded(Date recorded) {
        this.recorded = recorded;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Track track = (Track) o;

        if (album != null ? !album.equals(track.album) : track.album != null) return false;
        if (duration != null ? !duration.equals(track.duration) : track.duration != null) return false;
        if (file != null ? !file.equals(track.file) : track.file != null) return false;
        if (hash != null ? !hash.equals(track.hash) : track.hash != null) return false;
        if (live != null ? !live.equals(track.live) : track.live != null) return false;
        if (piece != null ? !piece.equals(track.piece) : track.piece != null) return false;
        if (recorded != null ? !recorded.equals(track.recorded) : track.recorded != null) return false;
        if (trackNumber != null ? !trackNumber.equals(track.trackNumber) : track.trackNumber != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (trackNumber != null ? trackNumber.hashCode() : 0);
        result = 31 * result + (album != null ? album.hashCode() : 0);
        result = 31 * result + (piece != null ? piece.hashCode() : 0);
        result = 31 * result + (live != null ? live.hashCode() : 0);
        result = 31 * result + (recorded != null ? recorded.hashCode() : 0);
        result = 31 * result + (duration != null ? duration.hashCode() : 0);
        result = 31 * result + (file != null ? file.hashCode() : 0);
        result = 31 * result + (hash != null ? hash.hashCode() : 0);
        return result;
    }


    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Track");
        sb.append("{trackNumber=").append(trackNumber);
        sb.append(", album=").append(album);
        sb.append(", piece=").append(piece);
        sb.append(", live=").append(live);
        sb.append(", recorded=").append(recorded);
        sb.append(", performances=").append(performances);
        sb.append(", duration=").append(duration);
        sb.append(", file=").append(file);
        sb.append(", hash='").append(hash).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
